package cn.toys.base.utils;

import java.math.BigDecimal;

public class BigDecimalSupport {

    public final static int DEFAULT_SCALE = 2;

    public static BigDecimal add(BigDecimal b1, BigDecimal b2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("scale must be positive or zero");
        }
        return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal add(BigDecimal b1, BigDecimal b2) {
        return add(b1, b2, DEFAULT_SCALE);
    }

    public static BigDecimal subtract(BigDecimal b1, BigDecimal b2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("scale must be positive or zero");
        }
        return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal subtract(BigDecimal b1, BigDecimal b2) {
        return subtract(b1, b2, DEFAULT_SCALE);
    }

    public static BigDecimal multiply(BigDecimal b1, BigDecimal b2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("scale must be positive or zero");
        }
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal multiply(BigDecimal b1, BigDecimal b2) {
        return multiply(b1, b2, DEFAULT_SCALE);
    }

}
